﻿/**********************************************************
 * ListBoxHandler.cs
 * This class acts as a pagination mechanism for the entities that are present in SHarepoint Lists
 * ********************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharePointExplorer.Classes
{
    public class ListBoxHandler
    {
        #region Declarations

        decimal totalCount;
        public int pageCount;
        
        List<Microsoft.SharePoint.Client.File> collection = null;
        List<Microsoft.SharePoint.Client.File> listFile = null;
        static int index;

        #endregion

        #region methods

        /// <summary>
        /// Sets the counters based on the file collection passed
        /// </summary>
        /// <param name="_collection">List of Sharepoint Files</param>
        public ListBoxHandler(List<Microsoft.SharePoint.Client.File> _collection)
        {
            totalCount = _collection.Count;
            decimal d = totalCount/12;
            pageCount = (int)decimal.Ceiling((decimal)d);
            collection = _collection;
            index = 0;
        }

        /// <summary>
        /// Gets only the first page of the file collection
        /// </summary>
        /// <returns>List of Sharepoint files</returns>
        public List<Microsoft.SharePoint.Client.File> GetFirstPageCollection()
        {
            if (totalCount >= 12)
                listFile = collection.GetRange(index, 12);
            else
                listFile = collection.GetRange(index, (int)totalCount);

            return listFile;
        }

        /// <summary>
        /// Gets the next set of files
        /// </summary>
        /// <returns>List of files</returns>
        public List<Microsoft.SharePoint.Client.File> GetNextPageCollection()
        {
            index += 12;

            if((totalCount - index)>=12)
                listFile = collection.GetRange(index, 12);
            else
                listFile = collection.GetRange(index, (int)(totalCount - index));

            

            return listFile;
        }

        /// <summary>
        /// Gets the previous set of files
        /// </summary>
        /// <returns>List of files</returns>
        public List<Microsoft.SharePoint.Client.File> GetPreviousPageCollection()
        {
            index -= 12;

            if(index>=0)
                listFile = collection.GetRange(index, 12);


            return listFile;
        }

        #endregion 
    }
}
